package yxy.game.pm2.module.shop.handle;

import org.joda.time.DateTime;

import yxy.cherry.data.bean.runningshop;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.message.ShopMessage;
import yxy.game.pm2.module.shop.CreditConquestShopModule;
import yxy.game.pm2.module.shop.Goods;
import yxy.game.pm2.module.shop.GoodsLimitType;

/** 商店 积分过关斩将商店 */
public class CreditConquestShopHandle extends ShopHandle<runningshop> {

	public Result buy(String playerCode, int id, int num, ShopMessage.CreditConquestShopBuyResponse.Builder response) {
		BuyResponse buyResponse = new BuyResponse();
		Result result = buy(playerCode, id, num, buyResponse);
		if (result.equals(Result.Success)) {
			response.setPurchase(buyResponse.getPurchase());
			response.setGoods(buyResponse.getGoods());
		}
		return result;
	}

	@Override
	public CreditConquestShopModule getModule(String playerCode) {
		CreditConquestShopModule module = CreditConquestShopModule.get(playerCode);
		DateTime now = DateTime.now();
		boolean needSet = false;
		for (Goods goods : module.getGoods()) {
			needSet |= update(goods, now);
		}
		if (needSet) {
			module.set();
		}
		return module;
	}

	@Override
	public GoodsLimitType limitType(runningshop data) {
		return GoodsLimitType.不限购;
	}

	@Override
	public int limit(runningshop data) {
		return 0;
	}

	@Override
	public int rebate(runningshop data) {
		return 0;
	}

	@Override
	public Currency cost(runningshop data, int num) {
		return Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() * num);
	}

	@Override
	public Currency purchase(runningshop data, int num) {
		return Currency.create(data.getShoptype(), data.getShopid(), data.getShopnum() * num);
	}

	@Override
	public Class<runningshop> clazz() {
		return runningshop.class;
	}
}